package com.yanqu.road.dao.impl.activity.commonteam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.commonteam.data.CommonTeamData;
import com.yanqu.road.entity.activity.commonteam.data.CommonTeamRankScoreData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class CommonTeamTeamDataDaoImpl extends TempDataObjectDao<CommonTeamData> {

    public static String getCommonTeamDataSql = "select * from `t_c_common_team` where `activityId`=? and groupId = ? and delFlag = 0";
    public Map<Integer, CommonTeamData> getCommonTeamDataMap(int activityId, int groupId) {
        Map<Integer, CommonTeamData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCommonTeamDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CommonTeamData data = parse(rs);
                map.put(data.getTeamId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getCommonMaxTeamIdSql = "select * from `t_c_common_team` where `activityId`=? and groupId = ? order by teamId desc limit 1";
    public int getCommonMaxTeamId(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCommonMaxTeamIdSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CommonTeamData data = parse(rs);
                return data.getTeamId();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            getDb().closeConnection(call, rs);
        }
        return 0;
    }

    public static String updateCommonTeamDataSql = "update `t_c_common_team` set serverId=?,teamName=?,leaderId=?,leaderName=?,score=?,rankScoreMap=?,delFlag=?,createTime=? where activityId=? and groupId = ? and teamId = ?";
    @Override
    protected boolean updateData(CommonTeamData commonTeamData) {
        commonTeamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonTeamData.getTeamName()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamData.getLeaderId()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonTeamData.getLeaderName()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(commonTeamData.getRankScoreMap())));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamData.getDelFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamData.getTeamId()));
        if (getDb().executeNoneQuery(updateCommonTeamDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCommonTeamDataSql = "insert into `t_c_common_team`(activityId,groupId,serverId,teamId,teamName,leaderId,leaderName,score,rankScoreMap,delFlag,createTime) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CommonTeamData commonTeamData) {
        commonTeamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamData.getTeamId()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonTeamData.getTeamName()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamData.getLeaderId()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonTeamData.getLeaderName()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(commonTeamData.getRankScoreMap())));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamData.getDelFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamData.getCreateTime()));
        if (getDb().executeNoneQuery(insertCommonTeamDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CommonTeamData parseData(ResultSet rs) throws SQLException {
        CommonTeamData commonTeamData = new CommonTeamData();
        commonTeamData.setActivityId(rs.getInt("activityId"));
        commonTeamData.setGroupId(rs.getInt("groupId"));
        commonTeamData.setServerId(rs.getLong("serverId"));
        commonTeamData.setTeamId(rs.getInt("teamId"));
        commonTeamData.setTeamName(rs.getString("teamName"));
        commonTeamData.setLeaderId(rs.getLong("leaderId"));
        commonTeamData.setLeaderName(rs.getString("leaderName"));
        commonTeamData.setScore(rs.getLong("score"));
        if (!StringUtils.isNullOrEmpty(rs.getString("rankScoreMap"))) {
            commonTeamData.setRankScoreMap(JSON.parseObject(rs.getString("rankScoreMap"), new TypeReference<Map<Integer, CommonTeamRankScoreData>>() {}));
        }
        commonTeamData.setDelFlag(rs.getInt("delFlag"));
        commonTeamData.setCreateTime(rs.getLong("createTime"));
        return commonTeamData;
    }
}
